<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :selectPer="'sh:order:jr:page'"
        :excelPer="'sh:order:jr:export'"
        @search="search"
        @handleDataExport="handleDataExport"
        :showSearchButton="true"
        :showExportButton="true"
        @export="excelInfo"
      >
        <el-date-picker
          v-model="form.createTime"
          type="datetimerange"
          start-placeholder="订单开始时间"
          end-placeholder="订单结束时间"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
        />
        <el-select clearable filterable
          style="width: 200px"
          v-model="form.carBrandId"
          placeholder="请选择品牌"
          @change="handleCarBrand"
        >
          <el-option
            v-for="item in allBrand"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
        <el-select
          clearable
          filterable
          style="width: 200px"
          v-model="form.carSeriesId"
          placeholder="请选择车系"
        >
          <el-option
            v-for="item in allSeries"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
        <el-input
          v-model="form.carOrderNo"
          clearable
          placeholder="订单号"
          style="width: 200px"
        />
        <el-input
          v-model="form.client"
          clearable
          placeholder="客户名称/手机号"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-edit"
            title="补充信息"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'sh:order:jr:supplementary'"
            @click="updateText(row)"
          ></i>
        </template>
      </data-table>
      <el-dialog
        title="补充信息"
        :visible.sync="visible"
        width="40%">
        <el-form :model="addForm" label-width="150px" :rules="addFormRules" ref="addForm">
          <el-row>
            <el-col :span="12">
              <el-form-item label="金融顾问:" prop="financeConsultant">
                <el-input v-model="addForm.financeConsultant" clearable placeholder="请输入金融顾问"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="金融单号:" prop="orderNo">
                <el-input v-model="addForm.orderNo" clearable placeholder="请输入金融单号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="放款日期:" prop="loanDate">
                <el-date-picker
                  style="width: 100%"
                  v-model="addForm.loanDate"
                  type="date"
                  placeholder="放款日期"
                  clearable
                  value-format="yyyy-MM-dd hh:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="贷款预计到账日期:" prop="arrivalDate">
                <el-date-picker
                  style="width: 100%"
                  v-model="addForm.arrivalDate"
                  type="date"
                  placeholder="贷款预计到账日期"
                  clearable
                  value-format="yyyy-MM-dd hh:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="按揭代办佣金:" prop="mortgageProxyCommission">
                <el-input v-model="addForm.mortgageProxyCommission" clearable placeholder="请输入按揭代办佣金"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="其他佣金:" prop="otherCommission">
                <el-input v-model="addForm.otherCommission" clearable placeholder="请输入其他佣金"></el-input>
              </el-form-item>
            </el-col>

          </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
         <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleOk">确 定</el-button>
        </span>
      </el-dialog>
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import {
  getList,
  updateData
} from '@/api/pre-sale/orderManagement/financial'
import { getDict, exportExcel } from '@/utils/index'
import { getAllBrand, getSeries } from '@/api/after-sale/systemManage/carModel'

export default {
  mixins: [
    baseMixin({
      idName: 'id',
      findData: getList,
      dataName: ['content', 'totalRecords'],
      format(res) {
        return res.map((data) => {
          data.loanStatus = data.loanStatus === 1 ? '未批复' : '已批复'
          data.paymentRatio = (data.paymentRatio * 10) + '%'
          return data
        })
      }
    })
  ],

  data() {
    return {
      visible: false,
      addForm: {
        id: '',
        arrivalDate: '',
        arrivalTime: '',
        financeConsultant: '',
        mortgageProxyCommission: '',
        orderNo: '',
        otherCommission: ''
      },
      addFormRules: {},
      getDict: getDict,
      form: {
        client: '' // 客户信息
      },
      tableColumnAttributes: [
        {
          name: '订单号',
          value: 'carOrderNo',
          type: 'text'
        },
        {
          name: '所属门店',
          value: 'storeName',
          type: 'text'
        },
        {
          name: '品牌',
          value: 'carBrandName',
          type: 'text'
        },
        {
          name: '车系',
          value: 'carSeriesName',
          type: 'text'
        },
        {
          name: '车型',
          value: 'carModelName',
          type: 'text'
        },
        {
          name: '车架号',
          value: 'vin',
          type: 'text'
        },
        {
          name: '客户姓名',
          value: 'clientName',
          type: 'text'
        },
        {
          name: '电话',
          value: 'clientPhone',
          type: 'text'
        },
        {
          name: '金融顾问',
          value: 'financeConsultant',
          type: 'text'
        },
        {
          name: '首付比例',
          value: 'paymentRatio',
          type: 'text'
        },
        {
          name: '首付款',
          value: 'downMoney',
          type: 'text'
        },
        {
          name: '贷款金额',
          value: 'loanAmount',
          type: 'text'
        },
        {
          name: '贷款状态',
          value: 'loanStatus',
          type: 'text'
        },
        {
          name: '制单人',
          value: 'salesCon',
          type: 'text'
        }
      ],
      dict: {},
      allBrand: [],
      allSeries: []
    }
  },
  methods: {
    async handleCarBrand(val) {
      val && await getSeries(val).then((res) => {
        const { success, data } = res
        if (success) this.allSeries = data
      })
    },
    // 查询
    search(form) {
      this.searchOptions = form
      this.currentPage = 1
      this.setTableData()
    },
    add() {
      this.$router.push({
        name: 'adddepositOrder',
        params: { type: 'add' }
      })
    },
    handleDataExport() {

    },
    // 补充信息
    updateText(row) {
      this.visible = true
      this.$nextTick(() => {
        this.addForm = {
          ...this.addForm,
          ...row
        }
      })
    },
    // 取消
    handleClose() {
      this.$refs.addForm.resetFields()
      this.visible = false
    },
    // 确认
    async handleOk() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.updateData()
        }
      })
    },
    // 补充信息接口
    async updateData() {
      await updateData({
        ...this.addForm
      }).then((res) => {
        const { success } = res
        if (success) this.setTableData()
        this.visible = false
      })
    },
    // 导出
    excelInfo() {
      exportExcel('JrOrder', '金融订单', { ...this.form }, this.pageSize, this.currentPage)
    }
  },
  async mounted() {
    await getAllBrand().then((res) => {
      const { success, data } = res
      if (success) this.allBrand = data
    })
  }
}
</script>
